package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.entity.ProductHierarchy;
import com.bytz.modules.cms.product.service.IAdminProductHierarchyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "产品分类列表")
@RequestMapping("/productHierarchy")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductHierarchy.EDIT + "')")
public class ProductHierarchyController {

    @Autowired
    private IAdminProductHierarchyService productHierarchyService;

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<ProductHierarchy> queryById(@RequestParam(value = "id") String id) {

        return Result.ok(productHierarchyService.getById(id));
    }

    @ApiOperation(value = "保存或编辑", notes = "保存或编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<ProductHierarchy> saveOrUpdate(@RequestBody ProductHierarchy model) {
        productHierarchyService.saveOrUpdateSaveObject(model, false);
        return Result.ok(model);
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ProductHierarchy>> page(ProductHierarchy productHierarchy,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {

        return Result.ok(productHierarchyService.queryPage(new Page<>(pageNo, pageSize), productHierarchy, req.getParameterMap()));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response,
                         HttpServletRequest request) throws Exception {

        export(new ProductHierarchy(), request, response);
    }

    @ApiOperation(value = "导入excel", notes = "导入excel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {

        List<ProductHierarchy> models = ExcelUtil.importGroup(request, ProductHierarchy.class, null);
        return Result.ok(productHierarchyService.importExcel(models));
    }

    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequestMapping(value = "/exportXls", method = RequestMethod.POST)
    public void export(ProductHierarchy productHierarchy,
                       HttpServletRequest request,
                       HttpServletResponse response) throws Exception {

        productHierarchyService.export(productHierarchy, request.getParameterMap(), response);
    }
}
